from django.http import HttpResponse
from django.shortcuts import render
import json

from src.gitRelation import *


default_data_file = 'data/data.json'

data_dict = {}
request_data = '' #前端请求的数据

def readDataFile():
    with open(default_data_file,'r',encoding='utf-8') as load_f:
        global data_dict
        data_dict = json.load(load_f)

# 覆盖
def writeDataFile():
    with open(default_data_file,"w",encoding='utf-8') as file:   #”w"代表着每次运行都覆盖内容
        file.write(request_data)

# 系统首页
def index(request):
    readDataFile()
    return render(request, 'index.html', {"data_dict": data_dict})

def save(request):
    if request.method == 'POST':
        postBody = request.body
        global request_data
        request_data = str(json.loads(postBody)).replace("\'", "\"")
        writeDataFile()

        # 提交到git
        pushData()

        return HttpResponse("success")
